// pages/orderDetail/orderDetail.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        order: {},
        imagePaths: [],
        doing: false,
        role: ''
    },

    repairImagePreview(e) {
        const idx = e.target.dataset.idx;
        wx.previewImage({
            current: this.data.imagePaths[idx],
            urls: this.data.imagePaths,
        });
    },

    getThisOrder: async (options, that) => {
        const idx = parseInt(options.idx);
        const doing = Boolean(options.doing);
        const str = doing ? 'now' : 'history';
        const order = getApp().globalData[`${str}Orders`][idx];
        if (!order) return false;
        that.setData({
            order: order,
            doing: doing
        });
        return true;
    },

    finishOrder() {
        wx.showModal({
            title: '提示',
            content: '维修已经完成了吗',
            complete: (res) => {
                if (res.confirm) {
                    wx.showLoading({
                        title: '加载中',
                        mask: true
                    });
                    wx.cloud.callFunction({
                        'name': 'finishOrder',
                        data: { orderID: this.data.order._id }
                    }).then(res => {
                        if (res.result !== true) throw "finish unsuccessfully";
                        this.setData({
                            doing: false
                        });
                        wx.showToast({
                            title: '维修已完成',
                            icon: 'success',
                            duration: 2000,
                            mask: true
                        });
                        setTimeout(() => {
                            wx.switchTab({
                                url: '../index/index',
                            });
                            wx.switchTab({
                                url: '../space/space',
                            });
                        }, 2000);
                    }).catch(err => wx.showToast({
                        title: '出错了',
                        icon: 'error'
                    }));
                }
            }
        })
    },

    cancelOrder() {
        wx.showLoading({
            title: '加载中',
            mask: true
        });
        wx.cloud.callFunction({
            name: 'cancelOrder',
            data: {
                oid: this.data.order._id
            }
        }).then(res => {
            if (res.result !== true) throw "cancel error";
            wx.showToast({
                title: '取消成功',
                icon: 'success',
                duration: 2000
            });
            setTimeout(() => wx.navigateBack(), 2000);
        }).catch(err => {
            console.error(err);
            wx.showToast({
                title: '取消失败',
                icon: 'error',
                duration: 2000
            });
            setTimeout(() => wx.navigateBack(), 2000);
        });
    },

    preCancel() {
        wx.showModal({
            title: '提示',
            content: '要取消报修吗',
            complete: (res) => {
                if (res.confirm) this.cancelOrder();
            }
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        wx.showLoading({
            title: '加载中',
            mask: true
        });
        this.getThisOrder(options, this).then((res) => {
            if (!res) {
                wx.showToast({
                    title: '当前无报修，3秒后返回',
                    icon: 'none',
                    mask: true,
                    duration: 3000
                });
                setTimeout(() => wx.switchTab({
                    url: '../index/index',
                }), 3000);
                return;
            }
            const db = wx.cloud.database();
            db.collection('repairImage').where({
                orderID: this.data.order._id
            }).get().then((res) => {
                const list = res.data.map(item => item.path);
                wx.cloud.getTempFileURL({
                    fileList: list,
                    success: rtn => {
                        this.setData({
                            imagePaths: rtn.fileList.map(item => item.tempFileURL),
                            role: getApp().globalData.user.role
                        });
                        setTimeout(() => wx.hideLoading(), 200);
                    },
                    fail: err => { throw err.errMsg; }
                });
            }).catch(err => {
                wx.showToast({
                    title: '出错了',
                    icon: 'error'
                });
                console.error(err);
            });
        }).catch(err => {
            wx.showToast({
                title: '出错了',
                icon: 'error'
            })
            console.error(err);
        });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})